package com.config;

//import com.service.TypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @author james-CAO
 * @title: IndexInterceptor
 * @projectName csm_emall_ssm
 * @description: 前台拦截器
 * @date 2020/9/2210:16
 */
public class IndexInterceptor extends HandlerInterceptorAdapter {

//    @Autowired
//    private TypesService typesService;

//    @Autowired
//    private CartService cartService;

    @Override
    public boolean preHandle(HttpServletRequest request , HttpServletResponse response ,Object hander) throws IOException {
        //为所有的页面设置类目列表
//        request.setAttribute("typeList",typesService.getList());
        //拦截指定路径
        String uri = request.getRequestURI();
        if (uri.contains("index/cart") || uri.contains("index/order") || uri.contains("index/my")) {
            Object user = request.getSession().getAttribute("user");
            if (Objects.isNull(user)) {
                response.sendRedirect("login");
                return false;
            }
        }
        //默认放过
        return true;

    }
}
